<script lang="ts" setup>
import { ref, onMounted } from "vue"
import { updateWebSiteConfigApi, getWebSiteConfigApi } from "@/api/webSiteConfig"
import { type UpdateWebSiteConfigRequestData } from "@/api/webSiteConfig/types"
import { type FormInstance, type FormRules, ElMessage } from "element-plus"
defineOptions({
  // 命名当前组件
  name: "WebSiteConfig"
})
const loading = ref<boolean>(false)

//#region 增
const DEFAULT_FORM_DATA: UpdateWebSiteConfigRequestData = {
  title: "",
  keywords: "",
  description: "",
  record_number: "",
  copyright: ""
}
const dialogVisible = ref<boolean>(false)
const formRef = ref<FormInstance | null>(null)
const formData = ref<UpdateWebSiteConfigRequestData>(JSON.parse(JSON.stringify(DEFAULT_FORM_DATA)))
const formRules: FormRules<UpdateWebSiteConfigRequestData> = {
  title: [{ required: true, trigger: "blur", message: "请输入站点名称" }]
}
const handleCreateOrUpdate = () => {
  formRef.value?.validate((valid: boolean, fields) => {
    if (!valid) return console.error("表单校验不通过", fields)
    loading.value = true
    updateWebSiteConfigApi(formData.value)
      .then(() => {
        ElMessage.success("操作成功")
      })
      .finally(() => {
        loading.value = false
      })
  })
}

//#region 查
const getWebSiteConfigDetail = () => {
  loading.value = true
  getWebSiteConfigApi()
    .then(({ data }) => {
      formData.value = data || DEFAULT_FORM_DATA
    })
    .catch(() => {
      formData.value = DEFAULT_FORM_DATA
    })
    .finally(() => {
      loading.value = false
    })
}
onMounted(() => {
  getWebSiteConfigDetail()
})
</script>

<template>
  <div class="app-container">
    <!-- 新增/修改 -->
    <el-card>
      <template #header>
        <div class="card-header">
          <h3>站点配置</h3>
        </div>
      </template>
      <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px" label-position="left">
        <el-form-item prop="title" label="站点标题">
          <el-input v-model="formData.title" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="keywords" label="站点关键词">
          <el-input v-model="formData.keywords" :min="1" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="description" label="站点描述">
          <el-input v-model="formData.description" placeholder="请输入" type="textarea" autosize />
        </el-form-item>
        <el-form-item prop="record_number" label="备案号">
          <el-input v-model="formData.record_number" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="copyright" label="站点版权">
          <el-input v-model="formData.copyright" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreateOrUpdate" :loading="loading">确认</el-button>
      </template>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
